#include "wds_ui_gameover.h"
#include "wds_ui_msgbox.h"
#include "wds_ui_helper.h"
#include "wds_sfx_manager.h"
#include "gen_game_state.h"

#include "admanager/admanager.h"
#include "nextpeer/np_manager.h"
#include "gameflow/game_flow_manager.h"
#include "gameflow/ui_popup.h"
#include "application/game_application.h"
#include "application/game_rms.h"
#include "lang/lang.h"

static WDSUIGameOver* g_Instance;

bool	RenderMe()
{
	g_Instance->Render();
	return true;
}


WDSUIGameOver::WDSUIGameOver() : 
					WDSUIPopupAnimator("ui/gameover.ui",""),				
					m_pUIPostFB(NULL),															
					m_pButtonNext(NULL),
					m_pButtonRetry(NULL),
					m_AlphaGlow(0),
					m_GlowAddVal(1)
{
	g_Instance = this;

	
	//m_ObjTurns.Load("img/score_number.lua",math::Vec2(0.5f,0.8f));

	if(m_pPopup)
	{
		
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("StarGray_1"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("StarGray_2"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("StarGray_3"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("Star_1"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("Star_2"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("Star_3"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("LabelYouFail"));

		//WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_glow"));
		//WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_play"));
		//WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_retry"));
		//WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_next"));
		


		m_pAnimateWidget = m_pPopup->GetWidget("background");
		m_pUIPostFB = m_pPopup->GetWidget("post_fb");
		if(m_pUIPostFB)
			m_pUIPostFB->SetIsVisible(false);
		
		WDSUIPopupAnimatorMgr::Instance().AddPopupAnimator(WDSUIPopupAnimatorMgr::POPUP_NAMES_GAMEOVER,this);

		m_Type = ANIMATION_SLIDEFROMTOP;
		m_pPopup->SetWidgetFunctor("button_play",UI_DELEGATE(WDSUIGameOver, OnButtonPlay));

		m_pButtonNext = m_pPopup->GetWidget("button_next");		
		m_pButtonRetry = m_pPopup->GetWidget("button_retry");
		m_pUIYouFailed = m_pPopup->GetWidget("LabelYouFail");
		m_pUIStarsFrame = m_pPopup->GetWidget("stars_frame");
		m_pUILevelText = m_pPopup->GetWidget("level_txt");
		m_pUIReason = m_pPopup->GetWidget("reason_txt");
		m_pUIButtonGlow = m_pPopup->GetWidget("button_glow");

		m_ObjScore.Load("ui_img/time_number.lua", m_pPopup->GetWidget("score") );

		char	Buffer[256];
		
		for(int i = 0; i < STAR_COUNT; i++)
		{
			sprintf(Buffer, "Star_%i", i+1);
			m_pUIStars[i] = m_pPopup->GetWidget(Buffer);
			m_StarPos[i] = m_pUIStars[i]->GetPosition(); 
		}
		
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_play"));
		WDSUIHelper::ResizeToCurentAspectRatio(m_pPopup->GetWidget("button_glow"));
		
        m_pButtons.push_back(new WDSUIHelper(m_pPopup->GetWidget("button_next")));	
		m_pButtons.push_back(new WDSUIHelper(m_pPopup->GetWidget("button_retry")));	
	}
}

WDSUIGameOver::~WDSUIGameOver()
{
	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		UTIL_SAFE_DELETE(m_pButtons[i]);
	}
	m_pButtons.resize(0);
}


void					WDSUIGameOver::Show()
{
	
}

void					WDSUIGameOver::Render()
{
	m_ObjScore.Render(GENGameState::GetGame()->GetGameWDS()->GetScale());
	//m_ObjTurns.Render(0.5f);

	
}


void					WDSUIGameOver::Tick(float dt)
{
	if(!m_pPopup->IsActive())
		return;
	
	if(m_Delay > 0)
	{
		m_Delay -= dt;
		return;
	}

	m_ObjScore.Tick(dt);
	
	for(int i = 0; i < (int)m_pButtons.size(); i++)
	{
		m_pButtons[i]->Tick(dt);
	}

	m_AlphaGlow += ( (float)m_GlowAddVal * dt);
	if(m_AlphaGlow > 1.0f)
	{
		m_GlowAddVal = -1;
		m_AlphaGlow = 1.0f;
	}

	if(m_AlphaGlow < 0.0f)
	{
		m_GlowAddVal = 1;
		m_AlphaGlow = 0.0f;
	}

	if(m_pUIButtonGlow)
		m_pUIButtonGlow->SetColor(1.0f,1.0f,1.0f,m_AlphaGlow);

	

		

}

void DELEGATE_CALL		WDSUIGameOver::OnButtonPlay(ui::UIWidget::WIDGET_EVENT Event, ui::UIWidget* pWidget)
{
	OnPressPlay();
	

}


void	WDSUIGameOver::OnPressPlay()
{
	
}

void					WDSUIGameOver::OnAnimatedUIFinish()
{
	
	

	
}

bool	WDSUIGameOver::KeyPressed(input::Keyboard::KEY* pKey)
{
	if(m_pPopup->IsActive() && pKey->KeyID == KEY_ESCAPE)
	{
		OnPressPlay();
		return true;
	}
	return false;
}




